﻿using Newtonsoft.Json;
using NPOI.XWPF.UserModel;
using ServiceReference2;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zocono.Infrastructure.Common;
using Zocono.WCS.Domain.DomainRepository;
using Zocono.WCS.Domain.EntityInfo;
using Zocono.WCS.Domain.EquipmentProcessDomain.PlanarGeneralMessage;
using Zocono.WCS.Domain.PlcBytesObject;
using Zocono.WCS.Domain.ProcessModuleDomain;
using Zocono.WCS.Infrastructure.ApllicationDto.FxModel;


namespace Zocono.WCS.Domain.FSJDomain
{
    public class FSJMesService:
        IFSJMesService,IPlanarGeneralPlcActionProcessService
    {
        ServiceReference1.EquipWebServiceActionClient EquipService = new ServiceReference1.EquipWebServiceActionClient();
        ServiceReference2.RPServicesClient RpServices = new ServiceReference2.RPServicesClient();

        //TODO 实现接口
        public async Task<PutMesResultDatasOutput> PutMesResultDatasAsync(PutMesResultDatasInput input)
        {
            if (input == null)
            {
                List<PutInData> putInDatas = new List<PutInData>();
                PutInData putInData = new PutInData();
                putInData.EquipCode = "P006262";
                putInData.CellCode = "ITCESHI1";
                putInData.Result = "OK";
                putInData.StartTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                putInData.NGReason = "";
                putInData.TrayCode = "1024";
                putInData.GradeName = "1";
                putInData.Position = "F";
                putInData.ResponseTime = DateTime.Now;
                putInData.CollectTime = DateTime.Now;
                putInData.QRCodeLevel = "OK";
                putInData.matchingResult = "OK";
                putInDatas.Add(putInData);

                //PutMesResultDatasInput input = new PutMesResultDatasInput();
                input.ProtocolNO = "2064";
                input.MsgNO = 12458;
                input.NeedReply = 1;
                input.CmdID = "501A";
                input.EID = "192.168.60.241";
                input.Data = putInDatas;
            }

            var json = JsonConvert.SerializeObject(input);

            var res = await EquipService.getLKFXResultAsync(json);
            var result = JsonConvert.DeserializeObject<PutMesResultDatasOutput>(res);
            return result;

        }
        public async Task<getLKFXResultOutput> getLKFXResultAsync(getLKFXResultInput input)
        {
            if (input == null)
            {
                var str = "ITCESHI1,ITCESHI2";

                input.EquipCode = "P006262";
                input.UploadTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                input.ProductCode = "C.O.000009828";
                input.CellCode = str.Split(',');
                input.WorkCode = "P-C-2020-020-002";
                input.IPaddress = "192.168.60.241";
            }
            

            var json = JsonConvert.SerializeObject(input);
            var res = await EquipService.getLKFXResultAsync(json);
            var result = JsonConvert.DeserializeObject<getLKFXResultOutput>(res);
            return result;

        }
        public async Task<getCellDataForFxOutput> getCellDataForFxAsync(getCellDataForFxInput input)
        {
            if (input == null)
            {
                input.WorkCode = "P-RC-2022-003-003";
                input.EquipCode = "P006262";
                input.ProductCode = "C.O.000009805";
                input.UploadTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                input.CellCode = "ITCESHI1";
                input.IPaddress = "192.168.60.241";
                input.MaterialCode = "1111";
            }
            

            var json = JsonConvert.SerializeObject(input);
            var res = await EquipService.getCellDataForFxAsync(json);
            var result = JsonConvert.DeserializeObject<getCellDataForFxOutput>(res);
            return result;

        }
        public async Task<upCartonDataOutput> upCartonDataAsync(upCartonDataInput input)
        {
            if (input == null)
            {
                List<Celldata> celldatas = new List<Celldata>();
                Celldata celldata = new Celldata();
                celldata.CellCode = "ITCESHI1";
                celldata.Location = "1";
                celldatas.Add(celldata);
                celldata.CellCode = "ITCESHI2";
                celldata.Location = "2";
                celldatas.Add(celldata);

                input.EquipCode = "P006262";
                input.UploadTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                input.ProductCode = "XAGH1A9675";
                //input.CellCode = "电芯编码";
                //input.WorkCode = "生产工作令";
                input.IPaddress = "192.168.60.241";
                input.PalletCode = "XAGH1A9675";
                input.Grade = "A-P-V1";
                input.BindTime = "2023-10-23 14:29:55";
                input.GradeStandardName = "L09-9811-132.5Ah-分选下仓";
                input.IsInbound = "1";
                input.NG_name = "REPT20257";
                input.Counts = "2";
                //input.Remark = "";
                //input.RemarkName = "";
                input.CellData = celldatas;
            }
            

            var json = JsonConvert.SerializeObject(input);
            var res = await EquipService.upCartonDataAsync(json);
            var result = JsonConvert.DeserializeObject<upCartonDataOutput>(res);
            return result;

        }

        public Task ProcessActionMessageAsync(BytesObjectBase plcActionMessage, BytesObjectBase wcsActionMessage, EquipmentDbObject planarEquipmentDbObject)
        {
            //TODO
            throw new NotImplementedException();
        }
    }
}
